package com.android.openapp.ui;

import java.util.ArrayList;

import com.android.openapp.services.Controller.ControllerBinder;
import com.android.openapp.R;

import data.Place;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//import android.widget.ArrayAdapter;
import android.widget.Button;

/**
 * Fragment che gestisce la visualizzazione di una lista di luoghi
 * @author Simone Panza
 *
 */
public class PlacesListFragment extends ListFragment{
	//ArrayAdapter<String> la;
	LocationAdapter locadapter;
	SimpleCursorAdapter mAdapter;
	
	MainActivity mActivity;
	Button mBut;
	
	/**
	 * empty constructor, as requested from Android documentation
	 */
	public PlacesListFragment(){
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.places_list, container, false);
		
		mBut = (Button) v.findViewById(R.id.listbutton);
		//TODO va implementato il Search dialog
		//tasto refresh
		mBut.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mActivity.mBinder!=null)
					updateNearPlaces(mActivity.mBinder);
			}
		});
		return v;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		mActivity = (MainActivity) getActivity();
		//prendo il binder ogni volta dalla MainActivity
		ControllerBinder mBinder = mActivity.mBinder;
		if (mBinder != null){
			updateNearPlaces(mBinder);
		}
	}
	
	public void updateNearPlaces(ControllerBinder binder){
		//metto 5000, ora non succede niente
		
		ArrayList<Place> list = binder.getNearPlaces(50000);
		locadapter= new LocationAdapter(mActivity, R.layout.locationlist, list);
		//la = new ArrayAdapter<String>(mActivity, R.layout.entry ,list);
		setListAdapter(locadapter);
	}
}
